home *** CD-ROM | disk | FTP | other *** search
/ DarkBASIC - The Ultimate 3D Game Creator / PCactive 8 CD1 - DarkBasic.iso / SOFTWARE / DEMOS / DarkForge2000 / mandeltrix / mandeltrix.dba next >
Encoding:
Text File  |  2000-03-24  |  2.5 KB  |  135 lines

  1. ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  2. `               Mandeltrix
  3. ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  4. ` By Rich Davey (rich@fatal-design.com)
  5. ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  6. ` Turns a mandlebrot  set into a matrix
  7. ` you can fly around in  with the mouse
  8. ` and arrow keys!
  9. `
  10. ` Music listened  to while  coding this
  11. ` Dirty  Trancing  DJ Mix by Timo Maas.
  12. ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  13.  
  14. sync rate 0
  15. sync on
  16. hide mouse
  17.  
  18. `    Calculate the mandelbrot and place into an array
  19.  
  20.     MinX#=-1.75; MaxX#=1.75; MinY#=-1.75; MaxY#=1.75
  21.  
  22.     dx#=(MaxX#-MinX#)/80
  23.     dy#=(Maxy#-MinY#)/60
  24.  
  25.     dim fractal#(4800)
  26.  
  27.     i=0
  28.  
  29.     for y=0 to 60-1
  30.         for x=0 to 80-1
  31.             pixel_color#=calc_pixel(MinX#+x*dx#,MinY#+y*dy#)*10
  32.             fractal#(i)=pixel_color#
  33.             inc i
  34.         next x
  35.     next y
  36.  
  37. `    Make a texture for the matrix (saves having a graphic image file)
  38.  
  39.     x1=0; y1=0; x2=0; y2=0; shade=0; boxsize=4; gridsize=16
  40.     create bitmap 1,boxsize*gridsize,boxsize*gridsize
  41.     set current bitmap 1
  42.  
  43.     for b=0 to gridsize
  44.  
  45.         for c=0 to gridsize
  46.             ink rgb(255,shade,0),rgb(0,0,0)
  47.             box x1,y1,x1+boxsize+3,y1+boxsize+3
  48.             x1=x1+boxsize
  49.             shade=shade+2
  50.         next c
  51.  
  52.         y1=y1+boxsize
  53.         x1=0
  54.  
  55.     next b
  56.  
  57.     get image 1,0,0,boxsize*gridsize,boxsize*gridsize
  58.     delete bitmap 1
  59.  
  60. `    Create the matrix and map the height according to the array
  61.  
  62.     mw=3000; mh=3000; gx=80; gy=60
  63.  
  64.     make matrix 1,mw,mh,gx,gy
  65.     prepare matrix texture 1,1,gridsize,gridsize
  66.     position matrix 1,0,0,0
  67.     set matrix wireframe on 1
  68.     color backdrop rgb(0,0,0)
  69.  
  70. `    Map the fractal to the matrix
  71.  
  72.     i=0
  73.  
  74.     for h=0 to gy-1
  75.         for w=0 to gx-1
  76.             set matrix height 1,w,h,fractal#(i)
  77.  
  78.             tilepic#=fractal#(i)
  79.             if tilepic#>boxsize*gridsize then tilepic#=boxsize*gridsize
  80.             set matrix tile 1,w,h,tilepic#
  81.  
  82.             inc i
  83.         next w
  84.     next h
  85.  
  86.     update matrix 1
  87.  
  88. `    Set-up the camera
  89.  
  90.     cx=mw/2; cy=400; cz=-200
  91.     position camera cx,cy,cz
  92.  
  93. `    The main loop
  94.  
  95.     do
  96.  
  97.         cx#=wrapvalue(cx#+mousemovey())
  98.         cy#=wrapvalue(cy#+mousemovex())
  99.         cz#=wrapvalue(cz#+mousemovez())
  100.         rotate camera cx#,cy#,cz#
  101.     
  102.         if upkey()=1 then move camera 25
  103.         if downkey()=1 then move camera -25
  104.  
  105.         sync
  106.  
  107.     loop
  108.  
  109. `    End
  110.  
  111.  
  112.  
  113. `    Fractal calculation function
  114. `    From the Dark Fractal code at www.darkforge.co.uk
  115.  
  116. function calc_pixel(ca#,cbi#)
  117.  
  118.     max_iteration=32
  119.     a#=0; b#=0; iteration#=0
  120.  
  121.     repeat
  122.         old_a# = a#
  123.         a# = a#*a# - b#*b# + ca#
  124.         b# = 2 * old_a#*b# + cbi#
  125.         length_z# = a#*a# + b#*b#
  126.  
  127.         inc iteration#
  128.  
  129.     until length_z#>4 or iteration#>max_iteration
  130.  
  131.     pixel_color#=iteration#
  132.  
  133. endfunction pixel_color#
  134.  
  135.